package org.example.now.serlizbale;

import java.io.*;

public class SerializableMain {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        // 当Father类为实现Serializable，启动时会报错，该类为实现Serializable
        // Father father = new Father();
        // 当Father类未实现Serializable，启动后，会得到0
        // 当Father类实现Serializable，启动后，会得到5
        Father father = new Son();
        father.f=5;

        // 序列化
        FileOutputStream fileOutputStream = new FileOutputStream("temp.o");
        ObjectOutput objectOutput = new ObjectOutputStream(fileOutputStream);
        objectOutput.writeObject(father);
        // 反序列化
        FileInputStream fileInputStream = new FileInputStream("temp.o");
        ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
        Object oject = objectInputStream.readObject();
        Father f = (Father) oject;
        // 由于子类没有f这个变量，是调用的父类的变量
        System.out.println(f.f);
        // 序列化时类的哪些字段会参与到序列化中？
        // 默认方式，java对象中的非静态和非transient的字段都会被定义为需要序列的字段
        // 另外一种方式是通过ObjectStreamField数组来声明类需要序列化的对象

    }
}
